//
//  ButtonViewController.swift
//  swift各类控件集锦
//
//  Created by iosdev1 on 2016/12/30.
//  Copyright © 2016年 zyy. All rights reserved.
//

import UIKit

class ButtonViewController: ZyBaseViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.setBtnOne()//第一个按钮
        self.setBtnTwo();

    }

    func setBtnOne() -> Void {
        let button = UIButton.init(frame: CGRect(x:10, y:100, width:100, height:100))
        self.view.addSubview(button)
        button.backgroundColor = UIColor.green
        button.setTitle("按钮属性", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.backgroundColor = UIColor.black
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left //按钮字体像左靠齐，但是挨着边上不符合大部分需求，重新定制
        button.addTarget(self, action: #selector(btnClick(btn:)), for: UIControlEvents.touchUpInside)

        let lblProgress = UILabel.init(frame: CGRect(x:100, y:30, width:100, height:100))
        self.view.addSubview(lblProgress)
        lblProgress.backgroundColor = UIColor.black;
        lblProgress.textColor = UIColor.white
        lblProgress.text = "测试KVO "

        //        text是UILabel的属性
        lblProgress.addObserver(self, forKeyPath: "text", options: NSKeyValueObservingOptions.new, context: nil)
        lblProgress.tag = 1000;
    }

    func btnClick(btn:UIButton) -> Void {
        let lbl:UILabel = self.view.viewWithTag(1000) as! UILabel
        lbl.text = "sdfsdf"
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "text" {
            zyUIshowMessage(message: "检测到kvo")
        }
    }

    override func viewDidDisappear(_ animated: Bool) {
        let lbl:UILabel = self.view.viewWithTag(1000) as! UILabel
        lbl.removeObserver(self, forKeyPath: "text")
    }






    func setBtnTwo() -> Void {
        let button = UIButton.init(frame: CGRect(x:60, y:300, width:200, height:100))
        self.view.addSubview(button)
        button.backgroundColor = UIColor.green
        button.setTitle("按钮属性", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.backgroundColor = UIColor.black
//        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left //按钮字体像左靠齐，但是挨着边上不符合大部分需求，重新定制
        button.setImage(UIImage.init(named: "tabbar_icon_chat_pre"), for: UIControlState.normal);

        //button图片的偏移量，距上左下右分别(10, 10, 10, 60)像素点
        button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 160);
        //button标题的偏移量，这个偏移量是相对于图片的
        button.titleEdgeInsets = UIEdgeInsetsMake(10, 100, 10, 10);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
